package com.request;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.Vector;

import com.database.LoginDb;
import com.database.ServerPaymentsDataBase;
import com.database.ServerSmsDataBase;
import com.database.ServerTransferDatabase;
import com.design.Layers;


public class ServerReceive extends Thread {
	private ServerSocket serverSocket = null;
	private Details details = null;
	private String[] serverInfo = new String[2];
	private String[] routerInfo = new String[2];
	private int port = 0;
	private String system = null;
	int routerPort=0;
	String routerSystem="";
	private Socket socket = null;;

	private ObjectInputStream objectInputStream = null;
	private StringTokenizer stringTokenizer = null;
	private SocketConnection socketConnection=null;
	private ObjectOutputStream objectOutputStream=null;
	private ServerResponseVO serverResponseVO=null;

	public int value=0;
	 private ServerSmsDataBase serverSmsDataBase=new ServerSmsDataBase();
	 private ServerPaymentsDataBase serverPaymentsDataBase=new ServerPaymentsDataBase();
	 private ServerTransferDatabase serverTransferDatabase=new ServerTransferDatabase();
	public ServerReceive() {
		socketConnection=new SocketConnection();
		serverResponseVO=new ServerResponseVO();
		details = new Details();
		routerInfo=details.Config("routerport","routersystem");
		routerPort=Integer.parseInt(routerInfo[0]);
		routerSystem=routerInfo[1];
		serverInfo = details.Config("serverport", "serversystem");
		port = Integer.parseInt(serverInfo[0]);
		system = serverInfo[1];
		start();
	}

	public String[] stringToken(String request) {
		String[] reqSplit = new String[2];
		stringTokenizer = new StringTokenizer(request, "/");
		while (stringTokenizer.hasMoreTokens()) {
			reqSplit[0] = stringTokenizer.nextToken();
			reqSplit[1] = stringTokenizer.nextToken();
		}
		return reqSplit;
	}

	public void run() {
		try {
			String[] reqInfo = new String[2];
			String request = null;
			serverSocket = new ServerSocket(Integer.parseInt(serverInfo[0]));
			while (true) {
				
				socket = serverSocket.accept();
				objectInputStream = new ObjectInputStream(socket
						.getInputStream());
				Object object = objectInputStream.readObject();
				
				
					ClientRequestVo clientRequestVo = (ClientRequestVo) object;
					//System.out.println(clientRequestVo.getRequest());
					String[] clientReq=clientRequestVo.getRequest().split(" ");
					++value;
					Vector table=new Vector();
					table.add(value);
					table.add(clientRequestVo.getSystem());
					table.add(clientRequestVo.getPort());
					table.add(clientRequestVo.getTransmissionRate());
					table.add(routerSystem);
					table.add(routerPort);
					table.add(clientReq[0]);
					System.out.println("***********");
					System.out.println(clientReq[0]);
					System.out.println("***********");
					Layers.mtab.addRow(table);
					if (clientReq[0].equalsIgnoreCase("balAval")) {
						String result="";
						result=serverSmsDataBase.SmsBalAval(clientReq[1]);
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						if(result.equals(""))
						{
							serverResponseVO.setResult("Invalid PinNumber");
						}
						else
						{
							serverResponseVO.setResult("Available Balance is "+result);	
						}
						
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("unsub"))
					{
						String result="";
						result=serverSmsDataBase.SmsUnsubscribeDb(clientReq[1]);
						if(result.equals(""))
						{
							serverResponseVO.setResult("Invalid AccountNumber");
						}
						else
						{
							serverResponseVO.setResult(result);	
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("sub"))
					{
						String result="";
						result=serverSmsDataBase.SmsSubscribeDb(clientReq[1]);
						if(result.equals(""))
						{
							serverResponseVO.setResult("Invalid AccountNumber");
						}
						else
						{
							serverResponseVO.setResult("Your Pin Number is "+result);	
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("laTran"))
					{
						String result="";
						
						result=serverSmsDataBase.SmsLastTransaction(clientReq[1]);
						if(result.equals(""))
						{
							serverResponseVO.setResult("Invalid Pin Number");
						}
						else
						{
							serverResponseVO.setResult(result);	
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("chqsts"))
					{
						String result="";
						result=serverSmsDataBase.SmsIssuedCheque(clientReq[1], clientReq[2]);
						if(!result.equals(""))
						{
							serverResponseVO.setResult(result);
						}
						
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("dchsts"))
					{
						String result="";
						result=serverSmsDataBase.SmsDepositedCheque(clientReq[1], clientReq[2]);
						if(result.equals(""))
						{
							serverResponseVO.setResult("Check the PinNumber & ChequeNumber");
						}
						else
						{
							serverResponseVO.setResult("Deposited Check Amount is "+result);	
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("chgpin"))
					{
						String result="";
						result=serverSmsDataBase.SmsChangePin(clientReq[1], clientReq[2]);
						if(result.equals(""))
						{
							serverResponseVO.setResult("Invalid PinNumber");
						}
						else
						{
							serverResponseVO.setResult("New Pin Number is "+result);	
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("login"))
					{
						String result="";
						LoginDb loginDb=new LoginDb();
						result=loginDb.LoginDatabase(clientReq[1], clientReq[2]);
						System.out.println("!!!!!!!!!!!!!!!!");
						System.out.println(result);
						System.out.println("!!!!!!!!!!!!!!!!");
						if(result.contains("Invalid"))
						{
							serverResponseVO.setError(result);
							serverResponseVO.setResult("failure");
						}
						else
						{
							serverResponseVO.setResult(result);
		
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);				
						
					}
					else if(clientReq[0].equalsIgnoreCase("REGISTER"))
					{
						String result="";
						String[] req=clientRequestVo.getRequest().split(" ");
						result=serverPaymentsDataBase.PaymentRegistration(req);
						if(result.equals(""))
						{
							serverResponseVO.setError("Pls check the Fields");
						}
						else
						{
							serverResponseVO.setResult(result);	
							
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("Payment"))
					{
						String[] result=new String[2];
						String[] req=clientRequestVo.getRequest().split(" ");
						result=serverPaymentsDataBase.paymentSubmit(req);
						
						if(!result[0].equals(""))
						{
							serverResponseVO.setResult(result[0]);
							//serverResponseVO.setResult("");	
						}
						else if(!result[1].equals(""))
						{
							serverResponseVO.setResult(result[1]);	
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("cheque"))
					{
						String result="";
						String[] req=clientRequestVo.getRequest().split(" ");
						result=serverPaymentsDataBase.ChequePayments(req);
						if(!result.equals(""))
						{
							serverResponseVO.setResult(result);	
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("own"))
					{
						String result="";
						System.out.println("PPPPPPPPPPPPPPPPPPPPP");
						System.out.println(clientRequestVo.getRequest());
						System.out.println("PPPPPPPPPPPPPPPPPPPPP");
						String[] req=clientRequestVo.getRequest().split(" ");
						
						result=serverTransferDatabase.OwnAccount(req);
						if(!result.equals(""))
						{
							serverResponseVO.setResult(result);	
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
					else if(clientReq[0].equalsIgnoreCase("search"))
					{
						String result="";
						String[] req=clientRequestVo.getRequest().split(" ");
						result=serverTransferDatabase.Search(req);
						if(!result.equals(""))
						{
							serverResponseVO.setResult(result);	
						}
						objectOutputStream=socketConnection.SocketSend(routerPort, routerSystem);
						serverResponseVO.setRequest(clientReq[0]);
						objectOutputStream.writeObject(serverResponseVO);
					}
				}
	
				

				} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		new ServerReceive();
	}
}
